<template>
  <el-container>
    <el-header class="no-flex" height="auto">
      <el-form ref="formRef" label-width="70px" inline  :model="searchForm">
        <el-form-item label="机构名称" prop="keyword"  >
          <el-input placeholder="请输入机构名称" clearable  style="width: 200px"  v-model="searchForm.name"/>
        </el-form-item>
        <el-form-item label="状态" prop="enabled"  >
          <el-select placeholder="请选择启用状态" clearable v-model="searchForm.enabled" style="width: 200px">
            <el-option v-for="item in dicts.system_global_status" :key="item.v" :label="item.k" :value="item.v"/>
          </el-select>
        </el-form-item>
        <el-form-item >
          <el-button type="primary" @click="upsearch" icon="el-icon-search">搜索</el-button>
          <el-button  @click="reset" icon="el-icon-refreshRight">重置</el-button>
        </el-form-item>
        <el-form-item style="display: flex; margin-bottom: 0">
          <el-button type="primary" icon="el-icon-plus" @click="add" v-auths = "['*:*:*','system:unit:add']">新增</el-button>
        </el-form-item>
      </el-form>
    </el-header>

    <el-main class="nopadding">
      <el-table ref="table" row-key="id" @selection-change="selectionChange" :data="tableData">
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column label="机构名称" prop="name" width="200"></el-table-column>
        <el-table-column label="负责人" prop="contact" width="80"></el-table-column>
        <el-table-column label="联系电话" prop="mobile" width="120"></el-table-column>
        <el-table-column label="地址" prop="address" width="300"></el-table-column>
        <el-table-column label="启用状态" prop="enabled" >
          <template #default="{row}">
            <template v-for="item in dicts.system_global_status">
              <el-tag v-if="row.enabled == item.v">{{item.k}}</el-tag>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" :formatter="rendererDateTime" ></el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="300">
          <template #default="scope">
            <el-button-group>
              <el-button  text type="primary"  @click="table_show(scope.row.id, scope.$index)" icon="el-icon-View" >查看</el-button>
              <el-button text type="primary"  v-if="scope.row.system!=1"  @click="table_edit(scope.row.id, scope.$index)" icon="el-icon-Edit" v-auths = "['*:*:*','system:user:update']">编辑</el-button>
              <el-popconfirm title="确定删除吗？" v-if="scope.row.system!=1" @confirm="table_del(scope.row, scope.$index)">
                <template #reference>
                  <el-button text type="danger" icon="el-icon-delete" v-auths = "['*:*:*','system:user:delete']">删除</el-button>
                </template>
              </el-popconfirm>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
  </el-container>

  <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>

</template>

<script>
import saveDialog from './components/save.vue'

export default {
  name: 'dept',
  dicts: ['system_global_status'],
  components: {
    saveDialog
  },
  data() {
    return {
      dialog: {
        save: false
      },
      selection: [],
      searchForm: {
        name: '',
        enabled: null
      },
      tableData:[],
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    // 请求数据
    getData(){
      this.$API.system.unit.list.get().then(res=>{
        this.tableData = this.$TOOL.dataToTree(res.data)
      })
    },
    // 转换时间
    rendererDateTime(row, column, cellValue, index){
      return this.$TOOL.dateFormat(cellValue)
    },
    //添加
    add(){
      this.dialog.save = true
      this.$nextTick(() => {
        this.$refs.saveDialog.open().setData()
      })
    },
    //编辑
    table_edit(id){
      this.dialog.save = true
      this.$nextTick(() => {
        this.$refs.saveDialog.open('edit').setData(id)
      })
    },
    //重置
    reset(){
      this.searchForm = {
          name: '',
          enabled: null
      };
      this.tableData = [];
      this.getData()
    },
    //查看
    table_show(id){
      this.dialog.save = true
      this.$nextTick(() => {
        this.$refs.saveDialog.open('show').setData(id)
      })
    },
    //删除
    async table_del(row){
      let res = await this.$API.system.unit.delUnit.get(row.id)
      console.log(res)
      if(res.code == 200){
        this.$message.success('删除成功')
        this.tableData = []
        this.getData()
      } else{
        this.$message.error('删除失败')
      }
    },
    //表格选择后回调事件
    selectionChange(selection){
      this.selection = selection;
    },
    //搜索
    async upsearch(){
      let res = await this.$API.system.unit.list.get(this.searchForm)
      this.tableData = this.$TOOL.dataToTree(res.data)
    },
    //根据ID获取树结构
    filterTree(id){
      var target = null;
      function filter(tree){
        tree.forEach(item => {
          if(item.id == id){
            target = item
          }
          if(item.children){
            filter(item.children)
          }
        })
      }
      filter(this.$refs.table.tableData)
      return target
    },
    //本地更新数据
    handleSaveSuccess(data, mode){
      if(mode =='add'){
        this.reset()
      }else if(mode =='edit'){
        this.reset()
      }
    }
  }
}
</script>

<style scoped>

</style>
